package cn.iocoder.yudao.module.crm.service.followup;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.crm.controller.admin.followup.vo.CrmFollowStageConfigSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.followup.CrmFollowStageConfigDO;
import cn.iocoder.yudao.module.crm.dal.mysql.followup.CrmFollowStageConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

@Service
@Validated
public class CrmFollowStageConfigServiceImpl implements CrmFollowStageConfigService {

    @Resource
    private CrmFollowStageConfigMapper followStageConfigMapper;

    @Override
    public Long createConfig(CrmFollowStageConfigSaveReqVO createReqVO) {
        CrmFollowStageConfigDO config = BeanUtils.toBean(createReqVO, CrmFollowStageConfigDO.class);
        followStageConfigMapper.insert(config);
        return config.getId();
    }

    @Override
    public void deleteConfig(Long id) {
        followStageConfigMapper.deleteById(id);
    }

    @Override
    public CrmFollowStageConfigDO getConfig(Long id) {
        return followStageConfigMapper.selectById(id);
    }

    @Override
    public Long updateConfig(CrmFollowStageConfigSaveReqVO createReqVO) {
        CrmFollowStageConfigDO config = BeanUtils.toBean(createReqVO, CrmFollowStageConfigDO.class);
        followStageConfigMapper.updateById(config);
        return config.getId();
    }

    @Override
    public List<CrmFollowStageConfigDO> getListByModuleId(Long moduleId) {
        List<Long> moduleIds = new ArrayList<>();
        if (ObjUtil.isNotEmpty(moduleId)) {
            moduleIds.add(moduleId);
            moduleIds.add(0L);
        }
        return followStageConfigMapper.selectList(new LambdaQueryWrapperX<CrmFollowStageConfigDO>()
                .inIfPresent(CrmFollowStageConfigDO::getModuleId, moduleIds)
                .orderByDesc(CrmFollowStageConfigDO::getSort));
    }

    @Override
    public List<CrmFollowStageConfigDO> getList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return followStageConfigMapper.selectBatchIds(ids);
    }

    @Override
    public Map<Long, CrmFollowStageConfigDO> getMap(Collection<Long> ids) {
        List<CrmFollowStageConfigDO> getList = getList(ids);
        if (CollUtil.isEmpty(getList)) {
            return Collections.emptyMap();
        }
        return CollectionUtils.convertMap(getList, CrmFollowStageConfigDO::getId);
    }

    @Override
    public void initFollowStageConfig() {
        List<CrmFollowStageConfigDO> list = TenantUtils.execute(0L, () -> followStageConfigMapper.selectList());
        if (CollUtil.isEmpty(list)) {
            return;
        }
        list.forEach(item -> item.setId(null));
        followStageConfigMapper.insertBatch(list);
    }
}
